package com.example.baselibs.base.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.baselibs.base.BaseFun;
import com.example.baselibs.router.path.ModuleMinePath;
import com.example.baselibs.utils.ActivityManager;
import com.example.baselibs.utils.ActivityUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity implements BaseFun {
    public Context context;
    //Butterknife绑定
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
        setContentView(getLayoutRes());
        initButterKnife();
        //初始化数据
        initData(savedInstanceState);
        initARouter();
        ActivityManager.getInstance().addActivity(this);
        Log.i("start","已经启动！！！");
    }

    /**
     * 初始化Butterknife
     */
    private void initButterKnife(){
        unbinder= ButterKnife.bind(this);
    }

    /**
     * 调用 inject 方法，如果传递过来的参数含有，这样使用 @Autowired 的会自动解析
     */
    private void initARouter(){
        ARouter.getInstance().inject(this);
    }
    public void toActivity(String path){
        ARouter.getInstance().build(path).navigation();
    }

    public Context getActivityContext(){
        return context;
    }

    public <T extends ViewModel> T getViewModel(Class<T> clz){
        return ViewModelProviders.of(this).get(clz);
    }

    /**
     * 启动Activity
     * @param clazz
     */
    public void startActivity(Class<?> clazz){
        Intent intent=new Intent(this,clazz);
        startActivity(intent);
    }

    /**
     * 带参数启动Activity
     * @param clazz
     * @param bundle
     */
    public void startActivity(Class<?> clazz,Bundle bundle){
        Intent intent=new Intent(this,clazz);
        if (bundle!=null){
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 含有Bundle通过Class打开编辑界面,并返回数据
     * @param clazz
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> clazz,Bundle bundle,int requestCode){
        Intent intent=new Intent(this,clazz);
        if (bundle!=null){
            intent.putExtras(bundle);
        }
        startActivityForResult(intent,requestCode);
    }

    /**
     * 简化Toast
     * @param msg
     */
    public void showToast(String msg){
        ActivityUtils.showToast(this,msg);
    }

    /**
     * 标题栏、状态栏隐藏
     */
    private void initStatus(){
        // 隐藏标题栏
        if (getSupportActionBar() != null)
            getSupportActionBar().hide();
        // 沉浸效果
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
        ActivityManager.getInstance().removeActivity(this);
    }
}
